﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class SPPAdd : Form
    {
        public SPPAdd()
        {
            InitializeComponent();
        }

        public SPPAdd(string stu_id, string stu_name)
        {
            InitializeComponent();
            txtInduk.Text = stu_id;
            txtNama.Text = stu_name;
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SPPAdd_Load(object sender, EventArgs e)
        {
            int year = DateTime.Now.Year;
            for (int i = year; i > year - 7; i--)
            {
                cmbTahun.Items.Add(i.ToString());
            }
        }

        private void txtJumlah_TextChanged(object sender, EventArgs e)
        {
            TextBox txtInput = (TextBox)sender;

            if (txtInput.TextLength != 0)
            {
                for (int i = 0; i < txtInput.TextLength; i++)
                {
                    if (Char.IsDigit(txtInput.Text, i))
                    {
                        // Do not do anything
                    }
                    else
                    {
                        MessageBox.Show("Harap gunakan angka !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        break;
                    }
                }
            }    
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            if (true == this.checkFieldValue())
            {
                string command = "SELECT * FROM SPP WHERE month='" + this.cmbBulan.SelectedItem.ToString() + "' AND year='" + this.cmbTahun.SelectedItem.ToString() + "'";
                DataTable dt = Database.getInstance().query(command);

                if (dt.Rows.Count == 1)
                {
                    MessageBox.Show("SPP di bulan dan tahun ini sudah ada !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                
                DataRow dataRow = dt.NewRow();

                dataRow["stu_id"] = this.txtInduk.Text;
                dataRow["year"] = int.Parse(this.cmbTahun.SelectedItem.ToString());
                dataRow["month"] = int.Parse(this.cmbBulan.SelectedItem.ToString());
                dataRow["amount"] = decimal.Parse(this.txtJumlah.Text);
                dataRow["remaining"] = decimal.Parse(this.txtJumlah.Text);
                dataRow["description"] = this.txtKeterangan.Text;

                dt.Rows.Add(dataRow);

                Database.getInstance().updateDB(dt);

                var result = MessageBox.Show("Tambah SPP berhasil !", "Sukses !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    this.Close();
                }
            }
        }

        private Boolean checkFieldValue()
        {
            if (this.cmbBulan.SelectedIndex == -1)
            {
                MessageBox.Show("Pilih bulan !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (this.cmbTahun.SelectedIndex == -1)
            {
                MessageBox.Show("Pilih tahun !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (this.txtJumlah.Text == "")
            {
                MessageBox.Show("Harap masukkan data jumlah !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            return true;
        }
    }
}
